﻿using System;
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kugar.Core.ExtMethod;

namespace Kugar.SL4.Core
{
    public delegate object SingleValueConverterDel(object value, Type targetType, object parameter, CultureInfo culture);
    //public delegate object SingleValueConvertBackDel(object value, Type targetType, object parameter, CultureInfo culture);


    public class SingleValueConverter: IValueConverter
    {
        public SingleValueConverter(SingleValueConverterDel converter)
            : this(converter,null)
        {
            
        }

        public SingleValueConverter(SingleValueConverterDel converter, SingleValueConverterDel backConverter)
        {
            Converter = converter;
            BackConverter = backConverter;
        }

        public SingleValueConverterDel Converter { get; private set; }

        public SingleValueConverterDel BackConverter { get; private set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (Converter!=null)
            {
                return Converter(value, targetType, parameter, culture);
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (BackConverter != null)
            {
                return BackConverter(value, targetType, parameter, culture);
            }
            else
            {
                return value;
            }
        }
        
    }

    /// <summary>
    ///     将输入的网址转换到相对应网站的绝对地址
    ///     如:
    ///         /Shop/dd.jpg   转换为: Http://localhost:3388/Shop/dd.jpg
    /// </summary>
    public class UrlConverterToWebRelative : IValueConverter
    {
        private static Regex reg=new Regex(@"^(https|http|ftp:\/\/).",RegexOptions.IgnoreCase);

        private static string localUrl = "";

        private const string sp = "{0}://{1}{2}";

        public UrlConverterToWebRelative()
        {
            if (string.IsNullOrEmpty(localUrl))
            {
                var s = Application.Current.Host.Source;

                if (s!=null)
                {
                    localUrl = string.Format(sp, s.Scheme, s.Host, s.Port == 0 ? "":":"+ s.Port);
                }
                
            }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Uri)
            {
                return value;
            }

            var str = value.ToStringEx().Trim();

            if (str.IsEmptyOrWhileSpace())
            {
                return value;
            }

            if (reg.IsMatch(str))
            {
                return new Uri(str, UriKind.Absolute);
            }
            else if(str.StartsWith("/"))
            {
                var tempUri = new Uri(localUrl + str, UriKind.Absolute);

                return tempUri;
            }
            else
            {
                return new Uri(str, UriKind.Relative); 
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Uri u = null;

            if (value is Uri)
            {
                u = (Uri) value;
            }
            else
            {

                var str = value.ToStringEx().Trim();

                if (str.IsEmptyOrWhileSpace())
                {
                    return value;
                }

                if (reg.IsMatch(str))
                {
                    u=new Uri(str, UriKind.Absolute);
                }
                else
                {
                    u = new Uri(str, UriKind.Relative);
                 
                }
                //u=new Uri(value.ToString());
            }

            return u.MakeRelativeUri(Application.Current.Host.Source).ToString();
        }
    }
}
